<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/14
 * Time: 15:52
 */

if (!defined('IN_IA')) {
    exit('Access Denied');
}

class Brands_MxShopPage extends WebPage {

    public function main() {

        global $_W,$_GPC;
        $uniacid = $_W['platformid'];

        $pindex = max(1, intval($_GPC['page']));
        $psize = 20;

        $condition = '';
        if (!empty($_GPC['keyword'])) {
            $condition .= " AND name LIKE '%{$_GPC['keyword']}%' ";
        }

        $status = intval($_GPC['status']);
        if (!empty($status)) {
            if ($status == 2) {
                $status2 = 0;
            } else {
                $status2 = $status;
            }
            $condition .= ' AND status=' . $status2;
        }

        $list = pdo_fetchall("SELECT * FROM " . tablename('mx_shop_service_goods_brands') . " WHERE deleted=0 $condition ORDER BY displayorder DESC  LIMIT " . ($pindex - 1) * $psize . ",{$psize}");

        $total = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('mx_shop_service_goods_brands') . " WHERE deleted=0 $condition ");
        $pager = pagination($total, $pindex, $psize);

        include $this->template();

    }

    public function post(){

        global $_W,$_GPC;
        $uniacid = $_W['platformid'];

        $id = intval($_GPC['id']);
        if(!empty($id)){
            $item  = pdo_fetch("SELECT * FROM " . tablename('mx_shop_service_goods_brands') . " WHERE id=" . $id . " AND deleted=0");
        }else{
            $item = array(
                'displayorder' => 0,
                'status' => 1
            );
        }

        if ($_W['ispost']) {
            if(empty($_GPC['name'])){
                show_json(0,'请输入设备品牌名称！');
            }

            $gbname = pdo_fetch("SELECT id FROM " . tablename('mx_shop_service_goods_brands') . " WHERE id != :id AND name = :gbname AND deleted=0", array(':id' => $id, ':gbname' => $_GPC['name']));
            if(!(empty($gbname['id']))){
                show_json(0,"该设备品牌名已存在");
            }
            
            if(empty($_GPC['logo'])){
                show_json(0,'请上传设备品牌logo！');
            }

            $data = array(
                'displayorder' => $_GPC['displayorder'],
                'uniacid' => $uniacid,
                'name' => $_GPC['name'],
                'logo' => $_GPC['logo'],
                'description' => $_GPC['description'],
                'status' => intval($_GPC['status']),
                'updatetime' => TIMESTAMP
            );

            if (empty($id)) {
                $data['createtime'] = $data['updatetime'];
                pdo_insert('mx_shop_service_goods_brands', $data);
                $id = pdo_insertid();
                plog('sgoods.brands.add', '新建"' . $data['name'] . '"品牌信息成功！设备品牌ID为' . $id);
            } else {
                pdo_update('mx_shop_service_goods_brands', $data, array('id' => $id));
                plog('sgoods.brands.add' . $data['name'] . '"品牌信息成功！设备品牌ID为' . $id);
            }
            show_json(1, array('url'=>webUrl('sgoods/brands')));

        }

        include $this->template();

    }

    public function status(){

        global $_W, $_GPC;

        $id = intval($_GPC['id']);
        $status = intval($_GPC['status']);

        if(!empty($id)){
            if($status == 1){
                pdo_update('mx_shop_service_goods_brands', array( 'status' => 1), array('id' => $id));
            }else{
                pdo_update('mx_shop_service_goods_brands', array( 'status' => 0), array('id' => $id));
            }
        }
        show_json(1, array('url'=>webUrl('sgoods/brands')));

    }

    public function brandsdel(){

        global $_W, $_GPC;

        $id = intval($_GPC['id']);
        if (empty($id)) {
            $id = is_array($_GPC['ids']) ? implode(',', $_GPC['ids']) : 0;
        }

        $goods = pdo_fetchall("SELECT id FROM " . tablename('mx_shop_service_goods') . " WHERE brands_id in( $id ) AND deleted=0 ");
        if(!empty($goods)){
            show_json(0, '要删除该品牌，先删除该品牌下的设备');
        }

        $cates = pdo_fetchall("SELECT * FROM " . tablename('mx_shop_service_goods_brands') . " WHERE id in( $id ) ");
        foreach ($cates as $cate) {
            pdo_update('mx_shop_service_goods_brands', array('deleted' => 1, 'status' => 0), array('id' => $cate['id']));
            plog('sgoods.brands.delete', "删除设备种类 <br/>设备种类信息:  ID: {$cate['id']}");
        }
        show_json(1, array('url' => referer()));

    }

}